﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using System.Data;


public partial class List : AdminBase
{

    private DataTable dc = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            grdbind();

    }

    public void grdbind()
    {
        using (Entities newlx = new Entities())
        {
            dc.Columns.Add("Classid", typeof(int));
            dc.Columns.Add("CpareID", typeof(int));
            dc.Columns.Add("ClassName", typeof(string));
         
            dc.Columns.Add("lb", typeof(string));
            var m = from n in newlx.S_Classs where n.SCpareID == 0 select n;
            foreach (var n in m)
            {
                DataRow row = this.dc.NewRow();
                row["Classid"] = int.Parse(n.SCID.ToString());
                row["CpareID"] = int.Parse(n.SCpareID.ToString());
           
                //if (n.CID.ToString() == m.ToList().Last().CID.ToString())
                //{
                //row["ClassName"] = "<img src='../images/tree_minusbottom.gif' align='absmiddle' border='0' /><img src='../images/tree_folder.gif' align='absmiddle' border='0' />" + n.CNmae.ToString();
                //}
                //else
                //{
                row["ClassName"] = "<img src='../50/images/tree_minusmiddle.gif' align='absmiddle' border='0' /><img src='../50/images/tree_folder.gif' align='absmiddle' border='0' />" + n.SCNmae.ToString();
            //}
                dc.Rows.Add(row);
                int leng = 1;
                dpbinder(dc, int.Parse(n.SCID.ToString()), leng);
            }
            //辅助修复项,查找父栏目ID错误的
            var all = newlx.L_Classs;

            foreach (var d in all)
            {
                if (d.CpareID != 0 && all.SingleOrDefault(p => p.CID == d.CpareID) == null)
                {
                    d.CpareID = 0;
                }
            }
            newlx.SaveChanges();
            this.rptList.DataSource = dc.DefaultView;
            rptList.DataBind();
        }
    }



    public void dpbinder(DataTable dp, int cID, int leng)//根据父ID查询子项
    {

        using (Entities newlx = new Entities())
        {

            int a;
            int.TryParse(cID.ToString(), out a);

            var mm = from nn in newlx.S_Classs where nn.SCpareID == a select nn;
            leng++;
            foreach (var n in mm)
            {
                string tr = addtab(leng);
                DataRow row = this.dc.NewRow();
                row["Classid"] = int.Parse(n.SCID.ToString());
               
             if (n.SCID.ToString() == mm.ToList().Last().SCID.ToString())
                {

                    row["ClassName"] = tr + "<img src='../50/images/tree_minusbottom.gif' align='absmiddle' border='0' /><img src='../50/images/tree_folder.gif' align='absmiddle' border='0' />" + n.SCNmae.ToString();
                }
                else
                {
                    row["ClassName"] = tr + "<img src='../50/images/tree_minusmiddle.gif' align='absmiddle' border='0' /><img src='../50/images/tree_folder.gif' align='absmiddle' border='0' />" + n.SCNmae.ToString();

                }

                dp.Rows.Add(row);
                dpbinder(dp, int.Parse(n.SCID.ToString()), leng);



            }
        }
    }
    private string addtab(int i)
    {
        string aa = "";
        for (int j = 1; j < i; j++)
        {
            aa += "<img src='../50/images//tree_line.gif' align='absmiddle' border='0' />";
        }
        return aa;
    }




    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);


        Entities bll = new Entities();
        S_Classs model = bll.S_Classs.First(p => p.SCID == id);

        switch (e.CommandName.ToLower())
        {

            case "del":
                if (model != null)
                {
                    //if (bll.S_Classs.Where(nw => nw.id == model.SCID).Count() == 0)
                    //{
                    //    bll.L_Classs.Remove(model);
                    //    var children = bll.L_Classs.Where(c => c.CpareID == model.CID);
                    //    if (children.Count() > 0)
                    //    {
                    //        foreach (var ch in children)//重置子栏目为一级栏目
                    //        {
                    //            ch.CpareID = 0;
                    //        }
                    //    }
                    //}
                    //else
                    //{
                 

                    //    Response.Write("<script> var myError=1;var Msg='<b>栏目非空！</b>，请先转移栏目下文章！！'</script>");

                    //}
                }
                break;

        }

        bll.SaveChanges();
        grdbind();

    }
}


